﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

namespace FreedomX2.Core.Transport.EmailProvider
{

    public class MineEmail
    {
        /// <summary>
        /// 我的名字
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 邮件通行证 eg:eddysoft@163.com
        /// </summary>
        public string passport { get; set; }
        /// <summary>
        /// 邮件密码 eg:aaaaaaa
        /// </summary>
        public string password { get; set; }

        /// <summary>
        /// 邮件服务器 eg:smtp.sina.com
        /// </summary>
        public string hostaddress { get; set; }

        /// <summary>
        /// 通过邮件通行证获得用户名
        /// </summary>
        public string username { get { if (passport.IndexOf('@') != -1)return passport.Split('@')[0]; else return "address error"; } }

    }
    public class EMail
    {
        private MineEmail mine;
        private MailMessage msg = null;
        private EMail() { MailMessage msg = new MailMessage(); }
        /// <summary>
        /// 制作一个邮件
        /// </summary>
        /// <param name="toPassport">多个收件人</param>
        /// <returns></returns>
        public static EMail Create(MineEmail me, params string[] toPassport)
        {
            EMail email = new EMail();
            email.msg.From = new MailAddress(me.passport, me.name);
            email.msg.IsBodyHtml = true;
            email.mine = me;
            foreach (string s in toPassport)
                email.msg.To.Add(new MailAddress(s, me.name));
            return email;
        }

        public void SetTitle(string title)
        {
            this.msg.Subject = title;
        }
        public void SetBody(string body)
        {
            this.msg.Body = body;
        }

        /// <summary>
        /// 文件地址中间用;分割
        /// </summary>
        /// <param name="filepachs"></param>
        public void SetAttachments(string filepachs)
        {
            string[] file = filepachs.Split(';');
            for (int n = 0; n < file.Length; n++)
            {
                if (file[n] != "")
                {
                    //附件对象
                    Attachment data = new Attachment(file[n], MediaTypeNames.Application.Octet);
                    //附件资料
                    ContentDisposition disposition = data.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(file[n]);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file[n]);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(file[n]);
                    //加入邮件附件
                    msg.Attachments.Add(data);
                }
            }
        }
        /// <summary>
        /// C# 发送邮件，通过测试sina，smtp服务器成功发送
        /// </summary>
        /// <param name="m_file">邮件附件的地址包括文件名称(多附件请用;分隔,单个附件请不要超过20M)</param>
        public void Send()
        {
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
            smtpClient.Host = mine.hostaddress;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new System.Net.NetworkCredential(mine.username, mine.password);
            try
            {
                smtpClient.Send(msg);//发送邮件
            }
            catch { }
        }
    }
}